package com.mondo.financialwallet.presentation.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.common.ui.BaseActivity;
import com.example.common.utils.TimeCount;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.entity.MobilePrefixEntity;
import com.mondo.financialwallet.data.params.SendMessageParams;
import com.mondo.financialwallet.presentation.ui.window.AreaSelectionPopupWindow;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;
import com.mondo.financialwallet.presentation.utils.Toasts;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 忘记密码
 */
public class ForgetPasswordActivity extends BaseActivity implements AreaSelectionPopupWindow.OnClickListener {

    @Bind(R.id.mobile_number_edit) EditText mMobileNumberEdit;
    @Bind(R.id.area_select_layout) View mAreaSelectLayout;
    @Bind(R.id.area_select_anchor_view) View mAreaSelectAnchorView;
    @Bind(R.id.area_number_text) TextView mAreaNumberTextView;
    @Bind(R.id.send_message_button) View mSendMessageButton;

    private ProgressDialog mSendingProgressDialog;
    private AreaSelectionPopupWindow mAreaSelectionPopupWindow;

    @Override
    protected int getBackgroundColor() {
        return Color.WHITE;
    }

    @Override
    protected void setStatusBar() {
        ActivityTitleBarHelper.setWhiteDarkColorStatus(this);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void initActions() {
        ActivityTitleBarHelper.init(this, R.string.title_forget_password);

        // 显示区号
        mAreaSelectLayout.setOnClickListener(v -> {
            if (mAreaSelectionPopupWindow != null) {
                mAreaSelectionPopupWindow.show(mAreaSelectAnchorView);
            }
        });

        // 发送短信
        mSendMessageButton.setOnClickListener(v -> requestMessageCode());

        // 手机区号
        requestMobileArea();
    }

    private void requestMobileArea() {
        DataRepository.get().mobilePrefix(result -> {
            if (result.isSuccess()) {
                List<MobilePrefixEntity.DataBean> list = result.getData();
                List<AreaSelectionPopupWindow.Area> areaList = new ArrayList<>();
                for (MobilePrefixEntity.DataBean dataBean : list) {
                    AreaSelectionPopupWindow.Area area = new AreaSelectionPopupWindow.Area(
                            dataBean.getEn_name(), dataBean.getMobile_prefix());
                    areaList.add(area);
                }

                if (mAreaSelectionPopupWindow == null) {
                    mAreaSelectionPopupWindow = new AreaSelectionPopupWindow(this, areaList);
                    mAreaSelectionPopupWindow.setOnClickListener(this);
                }
            }
        });
    }

    private void requestMessageCode() {
        String account = mMobileNumberEdit.getText().toString();
        if (TextUtils.isEmpty(account)) {
            Toasts.warning(R.string.message_please_input_account);
            return;
        }

        if (mSendingProgressDialog == null) {
            mSendingProgressDialog = ProgressDialog.show(this, null,
                    getString(R.string.message_sending_sms_verification_code));
            DataRepository.get().sendMessageByUser(account, result -> {
                mSendingProgressDialog.dismiss();
                mSendingProgressDialog = null;

                if (result.isSuccess()) {
                    GetVerificationCodeActivity.launch(this,
                            account, mAreaNumberTextView.getText().toString());
                } else {
                    Toasts.failure(result.getMsg());
                }
            });
        }
    }

    @Override
    public void onClick(String text) {
        mAreaNumberTextView.setText(text);
    }
}
